var wPrototype = {
    wdHeight: function () {
        var a, b = document;
        "number" == typeof window.innerWidth ? a = window.innerHeight : b.documentElement && b.documentElement.clientHeight ? a = b.documentElement.clientHeight : b.body && b.body.clientHeight && (a = b.body.clientHeight);
        return a
    },
    wdWidth: function () {
        var a, b = document;
        "number" == typeof window.innerWidth ? a = window.innerWidth : b.documentElement && b.documentElement.clientWidth ? a = b.documentElement.clientWidth : b.body && b.body.clientWidth && (a = b.body.clientWidth);
        return a
    },
    getElementTop: function (a) {
        var b =
            document;
        if (b.getElementById) var c = b.getElementById(a);
        else b.all && (c = b.all[a]);
        if (null != c) {
            yPos = c.offsetTop;
            for (tempEl = c.offsetParent; null != tempEl;) yPos += tempEl.offsetTop, tempEl = tempEl.offsetParent;
            return yPos
        }
        return 150
    },
    getElementWidth: function (a) {
        return document.getElementById(a).clientWidth
    },
    getElementLeft: function (a) {
        var b, c = document;
        c.getElementById ? b = c.getElementById(a) : c.all && (b = c.all[a]);
        a = b.offsetLeft;
        for (b = b.offsetParent; null != b;) a += b.offsetLeft, b = b.offsetParent;
        return a
    },
    scrollTop: function () {
        var a =
            document,
            b = a.body.scrollTop;
        0 == b && (b = window.pageYOffset ? window.pageYOffset : a.body.parentElement ? a.body.parentElement.scrollTop : 0);
        return b
    },
    scrollLeft: function () {
        var a = document,
            b = window.pageXOffset ? window.pageXOffset : 0,
            c = a.documentElement ? a.documentElement.scrollLeft : 0,
            a = a.body ? a.body.scrollLeft : 0,
            b = b ? b : 0;
        if (c && (!b || b > c)) b = c;
        return a && (!b || b > a) ? a : b
    },
    bdWidth: function () {
        var a = document;
        return bodyWidth = Math.max(Math.max(a.body.scrollWidth, a.documentElement.scrollWidth), Math.max(a.body.offsetWidth, a.documentElement.offsetWidth),
        Math.max(a.body.clientWidth, a.documentElement.clientWidth))
    },
    bdHeight: function () {
        var a = document;
        return Math.max(Math.max(a.body.scrollHeight, a.documentElement.scrollHeight), Math.max(a.body.offsetHeight, a.documentElement.offsetHeight), Math.max(a.body.clientHeight, a.documentElement.clientHeight))
    }
};
$(document).ready(function() {
	
	$( ".li_level_1" ).each(function( index ) {
		$(this).find('a').addClass('active');
		var width_li = $(this).width();
		$(this).css('width',width_li);
		$(this).find('a').removeClass('active');
	});
	
	$('.click_coupon').click(function() {
		 var check_form_dangky= $('.form_dangky').css('display');
		 if(check_form_dangky=="none"){
			 $('.form_dangky').slideDown();
		 }
		 else{
			  $('.form_dangky').hide();
			}
	});
	 $('.li_level_1').hover(
         function () {
           $(this).find('.level_2').show();
		   $(this).find('.a_level_1').addClass('active');
         }, 
         function () {
            $(this).find('.level_2').hide();
			$(this).find('.a_level_1').removeClass('active');
         }
     );
	var fix_width = 0;
	$( ".box_items_fix" ).each(function (i) {
       fix_width+= $(this).width();
    });
	$('#width_items').css('width',fix_width);
	$('.scroll-pane').jScrollPane({
		showArrows: true
	});
	
	var fix_width_1 = 0;
	$( ".items" ).each(function (i) {
       fix_width_1+= $(this).width()+37;
    });
	$('.width_items_1').css('width',fix_width_1);
	
	$('.scroll-pane_1').jScrollPane({
		showArrows: true
	});
	var fix_width_2 = 0;
	var b = 0;
	$( ".items_two" ).each(function (i) {
		fix_width_2+= $(this).width() + 35;
    });
	$('.width_items_2').css('width',fix_width_2);
	
	$('.scroll-pane_2').jScrollPane({
		showArrows: true
	});
	$('.a_level_1').eq(0).addClass('bg_0');
	$('.a_level_1').eq(1).addClass('bg_1');
	$('.a_level_1').eq(2).addClass('bg_2');
	$('.a_level_1').eq(3).addClass('bg_3');
	$('.a_level_1').eq(4).addClass('bg_4');
	
	var a = wPrototype.wdWidth();
	var b = wPrototype.bdHeight();
	var width_obj = $('#bg_form_muahang').width();
	console.log(width_obj);
	var pos_left = (a - width_obj)/2;
	$('.icon_buynow').click(function(){
		//Đặt biến cho các đối tượng để gọi dễ dàng
		var bg=$('div.bg_web');
		var obj=$('div#bg_form_muahang');
		var btnClose=obj.find('#popup_close');
		
		//Hiện các đối tượng
		bg.fadeIn(1000); //cho nền trong suốt
		obj.fadeIn(1000);
		obj.css('left',pos_left);
		bg.height(b);
		
		//Đóng popup khi nhấn nút
		btnClose.click(function(){
			bg.fadeOut(1000);
			obj.fadeOut(1000);
		
		});
		//Đóng popup khi nhấn background
		bg.click(function(){
			btnClose.click(); //Kế thừa nút đóng ở trên
		});
		//Đóng popup khi nhấn nút Esc trên bàn phím
		$(document).keydown(function(e){
			if(e.keyCode==27){
				btnClose.click(); //Kế thừa nút đóng ở trên
			}
		});
		return false;
	});
	
	$(".item_main_category ,.items ,.items_danhmuc").hover(
	  function () {		 
		$(this).addClass("border_hover");
	  },
	  function () {
		$(this).removeClass("border_hover");
	  }
	);
	$(".items_sp").hover(
	  function () {		 
		$(this).find('.name_items_items_sp').addClass("bg_hover");
	  },
	  function () {
		$(this).find('.name_items_items_sp').removeClass("bg_hover");
	  }
	);
	
	/* Search */
	$('.button-search').bind('click', function() {
		url = $('base').attr('href') + 'index.php?route=product/search';
				 
		var filter_name = $('input[name=\'filter_name\']').attr('value');
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('#header input[name=\'filter_name\']').bind('keydown', function(e) {
		if (e.keyCode == 13) {
			url = $('base').attr('href') + 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value');
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */
	$('#cart > .heading a').live('click', function() {
		$('#cart').addClass('active');
		
		$('#cart').load('index.php?route=module/cart #cart > *');
		
		$('#cart').live('mouseleave', function() {
			$(this).removeClass('active');
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).css('width', (columns * 143) + 'px');
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
	
	$('.success img, .warning img, .attention img, .information img').live('click', function() {
		$(this).parent().fadeOut('slow', function() {
			$(this).remove();
		});
	});	
});

function getURLVar(urlVarName) {
	var urlHalves = String(document.location).toLowerCase().split('?');
	var urlVarValue = '';
	
	if (urlHalves[1]) {
		var urlVars = urlHalves[1].split('&');

		for (var i = 0; i <= (urlVars.length); i++) {
			if (urlVars[i]) {
				var urlVarPair = urlVars[i].split('=');
				
				if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	
	return urlVarValue;
} 

function addToCart(product_id, quantity) {
	quantity = typeof(quantity) != 'undefined' ? quantity : 1;

	$.ajax({
		url: 'index.php?route=checkout/cart/add',
		type: 'post',
		data: 'product_id=' + product_id + '&quantity=' + quantity,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#cart-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}
function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/add',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#wishlist-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow');
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/add',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#compare-total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}